/* function generateFun(condition, nextState) {
  return function findNext(c) {
    if (condition === c) {
      return nextState
    } else {
      return start(c)
    }
  }
}

let foundE = generateFun('e', successed)
let foundD = generateFun('d', foundE)
let foundC = generateFun('c', foundD)
let foundB = generateFun('b', foundC)
let start = generateFun('a', foundB) */


function findx(condition, nextstate) {
  return function findNext(char) {
    if (char === condition) {
      return nextstate
    } else {
      return start(char)
    }
  }
}

let findE = findx('e', succeed);
let findD = findx('d', findE);
let findC = findx('c', findD)
let findB = findx('b', findC);
let start = findx('a', findB);
function findStr(str, pattern) {
  let state = start
  for (let i = 0; i < str.length; i++) {
    state = state(str[i])
    if (state === succeed) {
      return i - pattern.length + 1
    }
  }
  return -1
}


function succeed(c) {
  return succeed
}





module.exports = {
  findStr
}
